package com.ruoyi.gxs.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.SysDept;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.bean.BeanValidators;
import com.ruoyi.gxs.domain.GxsComprehensive;
import com.ruoyi.gxs.domain.GxsSecond;
import com.ruoyi.gxs.domain.GxsYearname;
import com.ruoyi.gxs.service.IGxsComprehensiveService;
import com.ruoyi.gxs.service.IGxsSecondService;
import com.ruoyi.gxs.service.IGxsYearnameService;
import com.ruoyi.system.mapper.SysDeptMapper;
import com.ruoyi.system.mapper.SysUserMapper;
import com.ruoyi.system.service.ISysConfigService;
import com.ruoyi.system.service.ISysDeptService;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.impl.SysUserServiceImpl;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.gxs.mapper.GxsAddstudentMapper;
import com.ruoyi.gxs.domain.GxsAddstudent;
import com.ruoyi.gxs.service.IGxsAddstudentService;

import javax.validation.Validator;

/**
 * 新增学生Service业务层处理
 * 
 * @author 高晓松
 * @date 2024-03-01
 */
@Service
public class GxsAddstudentServiceImpl implements IGxsAddstudentService 
{
    private static final Logger log = LoggerFactory.getLogger(GxsAddstudentServiceImpl.class);
    @Autowired
    private GxsAddstudentMapper gxsAddstudentMapper;

    @Autowired
    private ISysConfigService configService;

    @Autowired
    protected Validator validator;

    @Autowired
    private ISysDeptService sysDeptService;

    @Autowired
    private SysDeptMapper sysDeptMapper;

    @Autowired
    private ISysUserService sysUserService;

    @Autowired
    private IGxsComprehensiveService gxsComprehensiveService;

    @Autowired
    private IGxsYearnameService gxsYearnameService;

    @Autowired
    private IGxsSecondService gxsSecondService;


    /**
     * 查询新增学生
     * 
     * @param gxsId 新增学生主键
     * @return 新增学生
     */
    @Override
    public GxsAddstudent selectGxsAddstudentByGxsId(Long gxsId)
    {
        return gxsAddstudentMapper.selectGxsAddstudentByGxsId(gxsId);
    }

    /**
     * 查询新增学生列表
     * 
     * @param gxsAddstudent 新增学生
     * @return 新增学生
     */
    @Override
    public List<GxsAddstudent> selectGxsAddstudentList(GxsAddstudent gxsAddstudent)
    {
        return gxsAddstudentMapper.selectGxsAddstudentList(gxsAddstudent);
    }

    /**
     * 新增新增学生
     * 
     * @param gxsAddstudent 新增学生
     * @return 结果
     */
    @Override
    public int insertGxsAddstudent(GxsAddstudent gxsAddstudent)
    {
        String gxsClass = gxsAddstudent.getGxsClass();

        SysDept dept = sysDeptMapper.selectDeptByCalssName(gxsClass);

        //新建用户信息并且加入到学生中
        SysUser user = new SysUser();
        user.setUserName(gxsAddstudent.getUserName());
        user.setNickName(gxsAddstudent.getNickName());

        user.setPassword("123456");
        user.setPassword(SecurityUtils.encryptPassword(user.getPassword()));
        user.setDeptId(dept.getDeptId());
        Long[] roleIds = {100L};
        user.setRoleIds(roleIds);

        Long[] postTds = {};
        user.setPostIds(postTds);

        gxsAddstudentMapper.insertGxsAddstudent(gxsAddstudent);
        sysUserService.insertUser(user);

        SysUser sysUser = sysUserService.selectUserByUserName(gxsAddstudent.getUserName());
        //新建综测成绩
        GxsComprehensive gxsComprehensive = new GxsComprehensive();
        gxsComprehensive.setUserName(user.getUserName());
        gxsComprehensive.setNickName(user.getNickName());
        gxsComprehensive.setUserId(sysUser.getUserId());
        gxsComprehensive.setDeptId(user.getDeptId());

        //学年设定.按照当前拥有学年的最大学年
        GxsYearname gxsYearname = new GxsYearname();
        List<GxsYearname> gxsYearnames = gxsYearnameService.selectGxsYearnameList(gxsYearname);
        gxsYearname = gxsYearnames.get(gxsYearnames.size() - 1);
        gxsComprehensive.setGxsYear(gxsYearname.getGxsYearid());

        //设置默认的综测值
        gxsComprehensive.setGxsSchoolrecord(0L);
        gxsComprehensive.setGxsFail(0L);
        gxsComprehensive.setGxsThought(0L);
        gxsComprehensive.setGxsSportsscore(0L);
        gxsComprehensive.setGxsAppraisalScore(0L);


        //新建二课成绩
        GxsSecond gxsSecond = new GxsSecond();
        gxsSecond.setNickName(sysUser.getNickName());
        gxsSecond.setUserName(sysUser.getUserName());
        gxsSecond.setUserId(sysUser.getUserId());
        gxsSecond.setDeptId(sysUser.getDeptId());
        gxsSecond.setGxsScore(0L);

        gxsSecondService.insertGxsSecond(gxsSecond);

        int a = gxsComprehensiveService.insertGxsComprehensive(gxsComprehensive);

        return a;
    }

    /**
     * 修改新增学生
     * 
     * @param gxsAddstudent 新增学生
     * @return 结果
     */
    @Override
    public int updateGxsAddstudent(GxsAddstudent gxsAddstudent)
    {
        return gxsAddstudentMapper.updateGxsAddstudent(gxsAddstudent);
    }

    /**
     * 批量删除新增学生
     * 
     * @param gxsIds 需要删除的新增学生主键
     * @return 结果
     */
    @Override
    public int deleteGxsAddstudentByGxsIds(Long[] gxsIds)
    {
        return gxsAddstudentMapper.deleteGxsAddstudentByGxsIds(gxsIds);
    }

    /**
     * 删除新增学生信息
     * 
     * @param gxsId 新增学生主键
     * @return 结果
     */
    @Override
    public int deleteGxsAddstudentByGxsId(Long gxsId)
    {
        return gxsAddstudentMapper.deleteGxsAddstudentByGxsId(gxsId);
    }

    //用户导入
    @Override
    public String importStudent(List<GxsAddstudent> studentList, Boolean isUpdateSupport, String operName) {
        if (StringUtils.isNull(studentList) || studentList.size() == 0)
        {
            throw new ServiceException("导入用户数据不能为空！");
        }
        int successNum = 0;
        int failureNum = 0;
        int classNum = 0;
        StringBuilder successMsg = new StringBuilder();
        StringBuilder failureMsg = new StringBuilder();
        for (GxsAddstudent addstudent : studentList)
        {
            //判断班级是否存在
            boolean className = sysDeptService.selectStudentByUserName(addstudent.getGxsClass());
            boolean college = sysDeptService.selectStudentByUserName(addstudent.getGxsCollege());

            if (college){
                if (className) {
                    try
                    {
                        // 验证是否存在这个用户
                        GxsAddstudent studentName = gxsAddstudentMapper.selectStudentByUserName(addstudent.getUserName());
                        if (StringUtils.isNull(studentName))
                        {
                            //gxsAddstudentMapper.insertGxsAddstudent(addstudent);
                            insertGxsAddstudent(addstudent);
                            successNum++;
                            successMsg.append("<br/>" + successNum + "、学号 " + addstudent.getUserName() + " 导入成功");
                        }
                        else {
                            classNum++;
                            String msg = "<br/>" + addstudent.getUserName() + "  学生存在 " ;
                            failureMsg.append(msg );
                        }
                    }
                    catch (Exception e)
                    {
                        failureNum++;
                        String msg = "<br/>" + failureNum + "、账号 " + addstudent.getUserName() + " 导入失败：";
                        failureMsg.append(msg + e.getMessage());
                        log.error(msg, e);
                    }
                }
                else {
                    classNum++;
                    String msg = "<br/>" + addstudent.getGxsClass() + "  班级不存在 " ;
                    failureMsg.append(msg );
                }
            }
            else {
                classNum++;
                String msg = "<br/>" + addstudent.getGxsCollege() + "  学院不存在 " ;
                failureMsg.append(msg );
            }



        }
        if (classNum == 0){
            if (failureNum > 0)
            {
                failureMsg.insert(0, "很抱歉，导入失败！共 " + failureNum + " 条数据格式不正确，错误如下：");
                throw new ServiceException(failureMsg.toString());
            }
            else
            {
                successMsg.insert(0, "恭喜您，数据已全部导入成功！共 " + successNum + " 条，数据如下：");
            }
        }
        else {
            failureMsg.insert(0, "很抱歉，导入失败！共 " + classNum + " 条数据格式不正确，错误如下：");
            throw new ServiceException(failureMsg.toString());
        }
        return successMsg.toString();
    }
}
